package bible.sunflower.web.filter;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

public class BaseOnCookieSessionTimeOutFilter implements Filter {
	
	Logger log = Logger.getLogger( BaseOnCookieSessionTimeOutFilter.class );
	
	private ServletContext appContext = null;
	private String timeOutRedirectView = "";
	private SimpleDateFormat sdf = null;
	private String[] excludeUrls = null;

	public void destroy() {	

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest servletRequest = (HttpServletRequest)request;  
        HttpServletResponse servletResponse = (HttpServletResponse)response;  
        HttpSession session = servletRequest.getSession();
        boolean isExcludeUrl = false;
        String requestUrl = servletRequest.getRequestURI();
        for( String excludeUrl : excludeUrls ){
        	if( requestUrl.indexOf( excludeUrl ) >= 0 ){
        		isExcludeUrl = true;
        		break;
        	}
        }
        if( isExcludeUrl ){
        	chain.doFilter( request, response );
        }else if( session == null || appContext == null || session.getAttribute("custId") == null ){
        	servletResponse.sendRedirect( servletRequest.getContextPath() + "/" + timeOutRedirectView );
        }else{
        	log.debug("session expire time : " + session.getMaxInactiveInterval() );
        	Cookie cookie = new Cookie("app_last_visit_time", sdf.format( new Date()) );        	
        	cookie.setMaxAge( session.getMaxInactiveInterval() );        	
        	cookie.setPath("/");
        	servletResponse.addCookie( cookie );
        	chain.doFilter( request, response );
        }
	}

	public void init(FilterConfig config) throws ServletException {
		log.debug("current time : " + new Date() );
		appContext = config.getServletContext();
		timeOutRedirectView = config.getInitParameter("TimeOutRedirectView");
		log.debug(" time out view: " + timeOutRedirectView );
		TimeZone.setDefault( TimeZone.getTimeZone("Asia/Shanghai") );
		sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		log.debug("exclude_url: " + config.getInitParameter("exclude_url") );
		excludeUrls = config.getInitParameter("exclude_url").split(",");
	}
	
	public static void main(String[] args) {		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");	
		TimeZone.setDefault( TimeZone.getTimeZone("Asia/Shanghai") );
		//System.setProperty("user.timezone", "GMT+08");
		System.out.println( new Date() );
		System.out.println( sdf.format( new Date()) );
		//sdf.setTimeZone( TimeZone.getTimeZone("Asia/Shanghai") );
		System.out.println(sdf.format( new Date()) );
		System.out.println( System.getProperty("user.timezone") );
		System.setProperty("user.timezone", "Asia/Shanghai");
		System.out.println( System.getProperty("user.timezone") );
	}

}
